/*
 * Copyright 2009 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.core.search;

import java.util.Set;

import org.kommando.core.Displayable;
import org.kommando.core.util.Assert;

/**
 * {@link Result} implementation that keeps the score and the matched indexes.
 * <p>
 * The {@link #getToolTip()} and {@link #getName()} methods return html formatted text that reflects the scoring.
 * 
 * @author Peter De Bruycker
 */
public class ScoredResult<T extends Displayable> extends AbstractResult<T> {

    private final Set<Integer> matchedIndexes;
    private final float score;

    public ScoredResult(final T value, final float score, final Set<Integer> matchedIndexes) {
        super(value);

        Assert.argumentNotNull("matchedIndexes", matchedIndexes);

        this.score = score;
        this.matchedIndexes = matchedIndexes;
    }

    public Set<Integer> getMatchedIndexes() {
        return matchedIndexes;
    }

    public float getScore() {
        return score;
    }

    @Override
    public String getToolTip() {
        StringBuilder sb = new StringBuilder("<html><body>");

        sb.append("Score: <b>").append(score).append("<b>");

        return sb.toString();
    }

    @Override
    public String getHtmlFormattedName() {
        StringBuilder sb = new StringBuilder("<html><body><b>");

        char[] chars = getName().toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (matchedIndexes.contains(i)) {
                sb.append("<u>").append(chars[i]).append("</u>");
            } else {
                sb.append(chars[i]);
            }
        }

        sb.append("</b>");

        return sb.toString();
    }
}
